package com.amt.exception;

import com.amt.configure.ResultMsg;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**   
 * @Title: FinalExceptionHandlerController.java 
 * @Package com.amt.exception 
 * @Description: TODO(主要是解决未被全局异常处理的错误，
 * 如果设置资源映射文件不存在：spring.resources.add-mappings 会导致swagger druid等web页面不能访问) 
 * @author 江伟  
 * @date 2020年6月22日 下午5:12:29 
 * Copyright (c) ©1994-2021 Scjydz.com All Rights Reserved.
 */
@RestController
@Slf4j
@Api(tags="错误接口")
public class FinalExceptionHandlerController implements ErrorController {

	@Override
	public String getErrorPath() {
		 return "/error";
	}
	
	@GetMapping(value = "/error")
	@ResponseStatus(HttpStatus.NOT_FOUND)
	public Object error(HttpServletRequest request, HttpServletResponse response) {
		log.error("response error,httpCode:" + response.getStatus());
		// 错误处理逻辑
		int status = response.getStatus();
		if (status == 404) {
			return new ResultMsg(404, "无效的请求");
		} else if (status == 500) {
			return new ResultMsg(500, "服务器繁忙");
		} else if (status >= 100 && status < 200) {
			return new ResultMsg(100, "1XX错误");
		} else if (status >= 300 && status < 400) {
			return new ResultMsg(300, "3XX错误");
		} else if (status >= 400 && status < 500) {
			return new ResultMsg(400, "4XX错误");
		} else {
			return new ResultMsg(500, "网络故障");
		}
	}

}
